﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Reporting.WinForms;

using Willson.JsbManager.Entity;
using Willson.JsbManager.BllEntity;
using Willson.JsbManager.Comm;
using Willson.JsbManager.Bll;

namespace Willson.JsbManager.UI
{
    public partial class DeleGatePrintForm : Form
    {
        private int m_year, m_jd;
        private BindingList<BECompanyDeleGateInfo> DeleGateInfo_list = null;
        
        public DeleGatePrintForm(int year,int jd)
        {
            this.m_year = year;
            this.m_jd = jd;

            DeleGateInfo_list = BLLFactory<BLLCompanyCjhSum>.Instance.GetCompanyDeleGateInfoList(m_year, m_jd);

            InitializeComponent();
             
        }

        private void DeleGatePrintForm_Load(object sender, EventArgs e)
        {
            decimal sum = 0.00m;
            
            this.dataGridView.DataSource = DeleGateInfo_list;

            IEnumerator<BECompanyDeleGateInfo> ator = DeleGateInfo_list.GetEnumerator();

            while (ator.MoveNext())
            {
                if (ator.Current.Sel == 0) continue;

                sum = sum + Convert.ToDecimal(ator.Current.xxRmb);
            }

            this.label3.Text = "总金额 " + string.Format("{0:F2}", sum) + " 元";


            this.label1.Text = "共有 " + DeleGateInfo_list.Count.ToString() + " 条记录";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (DeleGateInfo_list.Count == 0) return;

            IEnumerator<BECompanyDeleGateInfo> ator = DeleGateInfo_list.GetEnumerator();

            PrintDataSet ds = new PrintDataSet();

            DataRow objRow;

            while (ator.MoveNext())
            {
                if (ator.Current.Sel == 0) continue;

                objRow = ds.Tables["DeleGatePrint"].NewRow();

                string tmp = StringOrder(ator.Current.xxRmb);

                objRow["DeleGateCode"] = ator.Current.DeleGateCode;
                objRow["CompanyName"] = ator.Current.CompanyName;
                objRow["Account"] = ator.Current.Account;
                objRow["BankName"] = ator.Current.BankName;
                objRow["Rmb"] = ator.Current.Rmb;
                objRow["xxRmb1"] = tmp[0].ToString();
                objRow["xxRmb2"] = tmp[1].ToString();
                objRow["xxRmb3"] = tmp[2].ToString();
                objRow["xxRmb4"] = tmp[3].ToString();
                objRow["xxRmb5"] = tmp[4].ToString();
                objRow["xxRmb6"] = tmp[5].ToString();
                objRow["xxRmb7"] = tmp[6].ToString();
                objRow["xxRmb8"] = tmp[7].ToString();
                objRow["xxRmb9"] = tmp[8].ToString();
                objRow["xxRmb10"] = tmp[9].ToString();

                ds.Tables["DeleGatePrint"].Rows.Add(objRow);
            }

            List<ReportParameter> listPara = new List<ReportParameter>();

            ReportParameter year = new ReportParameter("year", this.dateTimePicker.Value.Year.ToString());
            ReportParameter month = new ReportParameter("month", this.dateTimePicker.Value.Month.ToString());
            ReportParameter day = new ReportParameter("day", this.dateTimePicker.Value.Day.ToString());

            listPara.Add(year);
            listPara.Add(month);
            listPara.Add(day);

            XTPrintViewForm form = new XTPrintViewForm();
            form.Bind(ds, "DeleGatePrint", "Willson.JsbManager.UI.DeleGatePrint.rdlc", "PrintDataSet_DeleGatePrint", listPara);
            form.ShowDialog();
        }

        private string StringOrder(string str)
        {
            StringBuilder build = new StringBuilder("          ",10);

            int index1 = 0;

            for (int index = str.Length - 1; index >= 0 ; index--)
            {
                if (str[index] == '.') continue;

                build[index1] = str[index];

                index1++;
            }

            build[index1] = '￥';

            return build.ToString();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            PrintDataSet ds = new PrintDataSet();

            IEnumerator<BECompanyLetterPrint> ator = BLLFactory<BLLCompanyCjhSum>.Instance.GetCompanyLetterPrintInfoList(this.m_year, this.m_jd).GetEnumerator();

            DataRow objRow;

            while (ator.MoveNext())
            {
                objRow = ds.Tables["CompanyLetterPrint"].NewRow();

                objRow["PostCode"] = ator.Current.m_PostCode;
                objRow["Address"] = ator.Current.m_Address;
                objRow["Sjr"] = ator.Current.m_Sjr;

                ds.Tables["CompanyLetterPrint"].Rows.Add(objRow);
            }

            XTPrintViewForm form = new XTPrintViewForm();
            form.Bind(ds, "CompanyLetterPrint", "Willson.JsbManager.UI.CompanyLetterPrint.rdlc", "PrintDataSet_CompanyLetterPrint", null);
            form.ShowDialog();
        }

        private void dataGridView_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            if (dataGridView.Columns[e.ColumnIndex].Name == "Column6")
            {
                int m_company_id = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["CompanyID"].Value);
                decimal m_dj = BLLFactory<BLLCompanyCjhSum>.Instance.GetMaxDj(m_company_id);
                int cjh = Convert.ToInt32(dataGridView.Rows[e.RowIndex].Cells["Column6"].Value);

                this.dataGridView.Rows[e.RowIndex].Cells["Column5"].Value = string.Format("{0:F2}",m_dj * cjh);

                this.dataGridView.Rows[e.RowIndex].Cells["RMB"].Value = Rmb.CmycurD(m_dj * cjh);
            }

            if (dataGridView.Columns[e.ColumnIndex].Name == "Column5")
            {
                this.dataGridView.Rows[e.RowIndex].Cells["RMB"].Value = Rmb.CmycurD(Convert.ToDecimal(this.dataGridView.Rows[e.RowIndex].Cells["Column5"].Value));
            }
        }


    }
}
